﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SenseVoiceSharp
{
    public class CMVNFileParser
    {
        public string CmvnFile { get; set; }

        public CMVNFileParser(string cmvnFile)
        {
            CmvnFile = cmvnFile;
        }

        public void Parse(out List<string> meansList, out List<string> varsList)
        {
            meansList = new List<string>();
            varsList = new List<string>();

            // 读取文件所有行
            string[] lines = File.ReadAllLines(CmvnFile, Encoding.UTF8);

            for (int i = 0; i < lines.Length; i++)
            {
                string[] lineItems = lines[i].Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

                if (lineItems.Length > 0 && lineItems[0] == "<AddShift>")
                {
                    // 检查下一行是否是 <LearnRateCoef>
                    string[] nextLineItems = lines[i + 1].Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    if (nextLineItems.Length > 0 && nextLineItems[0] == "<LearnRateCoef>")
                    {
                        // 提取从第 4 个元素到倒数第二个元素
                        for (int j = 3; j < nextLineItems.Length - 1; j++)
                        {
                            meansList.Add(nextLineItems[j]);
                        }
                    }
                }
                else if (lineItems.Length > 0 && lineItems[0] == "<Rescale>")
                {
                    // 检查下一行是否是 <LearnRateCoef>
                    string[] nextLineItems = lines[i + 1].Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    if (nextLineItems.Length > 0 && nextLineItems[0] == "<LearnRateCoef>")
                    {
                        // 提取从第 4 个元素到倒数第二个元素
                        for (int j = 3; j < nextLineItems.Length - 1; j++)
                        {
                            varsList.Add(nextLineItems[j]);
                        }
                    }
                }
            }
        }
    }
}